package com.shanzhu.book.web;

import com.shanzhu.book.model.Notice;
import com.shanzhu.book.service.NoticeService;
import com.shanzhu.book.utils.PageUtils;
import com.shanzhu.book.utils.R;
import com.shanzhu.book.utils.TokenProcessor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户 控制层
 *
 * @author: ShanZhu
 * @date: 2023-12-31
 */
@RestController
@RequestMapping(value = "/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;



    /**
     * 查看用户信息
     *
     * @param token 用户token
     * @return 用户信息
     */
    @RequestMapping(value = "/info")
    public Map<String, Object> info(String token) {
        // 从redis中取用户
        Notice notice = noticeService.getNotice(token);
        if (notice == null) {
            // 获取失败
            return R.getResultMap(420, "获取用户信息失败");
        } else {
            // 获取成功
            return R.getResultMap(200, "获取用户信息成功", notice);
        }
    }

    /**
     * 退出登录
     *
     * @param token 用户token
     * @return 结果
     */
    @RequestMapping(value = "/logout")
    public Map<String, Object> logout(String token) {
        // 从redis中移除用户
        noticeService.removeNotice(token);
        return R.getResultMap(200, "退出登录成功");
    }




    /**
     * 获得数量
     *
     * @return 数量
     */
    @GetMapping(value = "/getCount")
    public Integer getCount() {
        return noticeService.getCount();
    }

    /**
     * 查询所有用户
     * @return 用户
     */
    @GetMapping(value = "/queryNotices")
    public List<Notice> queryNotices() {
        return noticeService.queryNotices();
    }


    /**
     * 分页查询用户
     *
     * @param params {page, limit, noticename}
     * @return 用户信息
     */
    @GetMapping(value = "/queryNoticesByPage")
    public Map<String, Object> queryNoticesByPage(@RequestParam Map<String, Object> params) {
        PageUtils.parsePageParams(params);
        int count = noticeService.getSearchCount(params);
        List<Notice> notices = noticeService.searchNoticesByPage(params);
        return R.getListResultMap(0, "success", count, notices);
    }

    /**
     * 添加用户
     *
     * @param notice 用户信息
     * @return 结果
     */
    @PostMapping(value = "/addNotice")
    public Integer addNotice(@RequestBody Notice notice) {
        return noticeService.addNotice(notice);
    }

    /**
     * 删除用户
     *
     * @param notice 用户信息
     * @return 结果
     */
    @DeleteMapping(value = "/deleteNotice")
    public Integer deleteNotice(@RequestBody Notice notice) {
        return noticeService.deleteNotice(notice);
    }

    /**
     * 批量删除用户
     *
     * @param notices 用户信息
     * @return 结果
     */
    @DeleteMapping(value = "/deleteNotices")
    public Integer deleteNotices(@RequestBody List<Notice> notices) {
        return noticeService.deleteNotices(notices);
    }

    /**
     * 更新用户
     *
     * @param notice 用户信息
     * @return 结果
     */
    @RequestMapping(value = "/updateNotice")
    public Integer updateNotice(@RequestBody Notice notice) {
        return noticeService.updateNotice(notice);
    }

}
